package com.frontend.screens;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.data.documents.DoctorsOrder;
import com.data.documents.TreatmentRecord;
import com.frontend.ConnectionException;
import com.frontend.DoctorsOfficeClient;
import com.users.User;

public class DoctorsOrderScreen extends Screen{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7157408140014970561L;
	private JLabel message, labWorkLabel, prescriptionLabel, followUpLabel, instructionsLabel;
	private JTextField labWork, prescription, followUp, instructions;
	private JPanel labWorkPanel, prescriptionPanel, followUpPanel, instructionsPanel;
	private JPanel b1,b2,b3,b4,b5, b6;
	private JButton submit, back;
	private boolean fadeout, fadein, block;
	private javax.swing.Timer timer;
	private int cycles;
	private DoctorsOrder d;
	private User u;
	private Screen prevScreen;
	

	public DoctorsOrderScreen(DoctorsOfficeClient window, DoctorsOrder d, User u, TreatmentRecord tr, Screen prevScreen){
		super(window);
		this.prevScreen = prevScreen;
		this.d  = d;
		this.u = u;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		DoctorsOrderListener dol = new DoctorsOrderListener();
		timer = new javax.swing.Timer(50, new AnimationListener());
		fadeout = false;
		fadein = false;
		block = true;
		
		b1 = new JPanel();
		b1.setOpaque(false);

		message = new JLabel("Doctors Orders");
		message.setAlignmentX(CENTER_ALIGNMENT);
		
		b2 = new JPanel();
		b2.setOpaque(false);
		
		labWorkPanel = new JPanel();
		labWorkPanel.setOpaque(false);
		labWorkPanel.setLayout(new BoxLayout(labWorkPanel, BoxLayout.X_AXIS));
		labWorkLabel = new JLabel("Lab Work Performed: ");
		labWork = new JTextField();
		labWork.addActionListener(dol);
		labWorkPanel.add(labWorkLabel);
		labWorkPanel.add(labWork);
		
		b3 = new JPanel();
		b3.setOpaque(false);

		prescriptionPanel = new JPanel();
		prescriptionPanel.setOpaque(false);
		prescriptionPanel.setLayout(new BoxLayout(prescriptionPanel, BoxLayout.X_AXIS));
		prescriptionLabel = new JLabel("Prescription: ");
		prescription = new JTextField();
		prescription.addActionListener(dol);
		prescriptionPanel.add(prescriptionLabel);
		prescriptionPanel.add(prescription);
		
		b4 = new JPanel();
		b4.setOpaque(false);
		
		followUpPanel = new JPanel();
		followUpPanel.setOpaque(false);
		followUpPanel.setLayout(new BoxLayout(followUpPanel, BoxLayout.X_AXIS));
		followUpLabel = new JLabel("Follow Up: ");
		followUp = new JTextField();
		followUp.addActionListener(dol);
		followUpPanel.add(followUpLabel);
		followUpPanel.add(followUp);
		
		b5 = new JPanel();
		b5.setOpaque(false);
		
		instructionsPanel = new JPanel();
		instructionsPanel.setOpaque(false);
		instructionsPanel.setLayout(new BoxLayout(instructionsPanel, BoxLayout.X_AXIS));
		instructionsLabel = new JLabel("Other Instructions: ");
		instructions = new JTextField();
		instructions.addActionListener(dol);
		instructionsPanel.add(instructionsLabel);
		instructionsPanel.add(instructions);
		
		b6 = new JPanel();
		b6.setOpaque(false);
		
		labWork.setText(d.getLabWorkPerformed());
		prescription.setText(d.getPrescription());
		followUp.setText(d.getFollowUpInstructions());
		instructions.setText(d.getOtherInstructions());
		
		submit = new JButton("Submit");
		back = new JButton("Back");
		submit.addActionListener(dol);
		back.addActionListener(dol);
		submit.setOpaque(false);
		back.setOpaque(false);
		
		add(b1);
		add(message);
		add(b2);
		add(labWorkPanel);
		add(b3);
		add(prescriptionPanel);
		add(b4);
		add(followUpPanel);
		add(b5);
		add(instructionsPanel);
		add(b6);
		add(submit);
		add(back);
		
		
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			message.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));
			labWorkLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			labWork.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			labWork.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			prescriptionLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			prescription.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			prescription.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			followUpLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			followUp.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			followUp.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			instructionsLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			instructions.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			instructions.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			
			submit.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			submit.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			back.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			back.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}
	
	private class DoctorsOrderListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == back){
				window.changeScreen(prevScreen);
				prevScreen.revalidate();
				prevScreen.repaint();
			}
			else{
				d.setLabWorkPerformed(labWork.getText());
				d.setPrescription(prescription.getText());
				d.setFollowUpInstructions(followUp.getText());
				d.setOtherInstructions(instructions.getText());
				block = false;
				
				if(d.getLabWorkPerformed().equals("") && d.getPrescription().equals("") 
						&& d.getFollowUpInstructions().equals("") && d.getOtherInstructions().equals(""))
				{
					timer.stop();
					block = true;
					fadeout = true;
					cycles = 0;
					message.setText("At least one field must be filled");
					message.setForeground(Color.red);
					timer.start();
				}
				
				if(!block){
						try {
							window.getNet().updateDocument(d,u.getUsername());
							timer.stop();
							block = true;
							fadeout = true;
							cycles = 0;
							message.setText("Doctors Order Submitted!");
							message.setForeground(Color.red);
							timer.start();
						} catch (ConnectionException e1) {
							window.getLogger().log(Level.WARNING, "Submitting Doctors Orders Failed.");
							e1.printStackTrace();
						}
						
				}
			}
		}
	}
	
	private class AnimationListener implements ActionListener {
		private int alphaVal;

		@Override
		public void actionPerformed(ActionEvent e) {
			if (cycles == 0)
				alphaVal = 255;
			cycles++;

			if (cycles > 20) {
				if (fadeout) {
					alphaVal = alphaVal - 10;
					if (alphaVal < 0)
						alphaVal = 0;
					message.setForeground(new Color(255, 0, 0, alphaVal));
					if (alphaVal == 0) {
						fadeout = false;
						fadein = true;
						message.setText("Make an Appointment");
					}
				}
				if (fadein) {
					alphaVal += 10;
					if (alphaVal > 255)
						alphaVal = 255;
					message.setForeground(new Color(0, 0, 0, alphaVal));
					if (alphaVal == 255) {
						fadein = false;
						timer.stop();
					}
				}
			}
		}

	}


}
